package com.qf.flash.ui.business;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.qf.flash.R;
import com.qf.flash.base.BaseRefreshFragment;
import com.qf.flash.constant.FlashCodes;
import com.qf.flash.model.GameGroupBean;
import com.qf.flash.mvp.contract.OrderHallContract;
import com.qf.flash.mvp.presenter.OrderHallPresenter;
import com.qf.flash.ui.business.adapter.OrderHallItem;
import com.qf.flashlib.adapter.RVAdapter;
import com.qf.flashlib.app.FlashLib;
import com.qf.flashlib.exception.ApiException;
import com.qf.flashlib.widget.ViewUtil;

import java.util.List;

/**
 * 创建日期：2020/3/14 on 20:07
 * 描述:
 * 作者:戴旭东
 */
public class OrderHallFragment extends BaseRefreshFragment implements RVAdapter.OnItemChildClickListener, OrderHallContract.View {
    private RVAdapter<GameGroupBean> mAdapter;

    private OrderHallPresenter mPresenter;

    @Override
    public RecyclerView.LayoutManager getLayoutManager() {
        return new LinearLayoutManager(getActivity());
    }

    @Override
    public RecyclerView.Adapter getRecyclerAdapter() {

        mAdapter = new RVAdapter<>(new OrderHallItem());
        mAdapter.setOnItemChildClickListener(this);
        return mAdapter;
    }

    @Override
    protected void initInject() {
        mPresenter = new OrderHallPresenter();
        initPresenters(mPresenter);
    }

    @Override
    protected void lazyLoad() {

        mRefresh.autoRefresh();
    }


    @Override
    protected void requestData(boolean isRefresh, int page) {
        mPresenter.getGameList(page);

    }

    @Override
    public void onItemChildClick(View view, int position) {
        switch (view.getId()) {
            case R.id.tv_jiedan:
                mPresenter.receiveOrRejectOrder(mAdapter.getData(position).getId(), FlashCodes.ORDER_RECEIVE);
                break;
            case R.id.cv_group:
                GameGroupBean gameGroupBean = mAdapter.getData(position);
                OrderDetailActivity.start(getActivity(), gameGroupBean.getPlayId(), gameGroupBean.getId(), false);
                break;
        }
    }

    @Override
    public void setGameList(List<GameGroupBean> data, int totalPage) {
        ViewUtil.setRefreshComplete(mRefresh, isRefresh);
        mRefresh.setEnableLoadMore(page < totalPage);
        if (isRefresh) {
            mAdapter.setDatas(data);
        } else {
            mAdapter.addData(data);
        }
    }

    @Override
    public void receiveOrRejectOrderSuccess(int optionType) {
        FlashLib.showToast("接单成功");
        mRefresh.autoRefresh();
    }

    @Override
    public void onError(ApiException e) {
        super.onError(e);
        ViewUtil.setRefreshComplete(mRefresh, isRefresh);
    }
}
